﻿// john?
//
// Vimeo C# Simple API Library Wrapper shizzle v0.1
// Written by a man ith a beard called Steve Hearn.
// (c) 2008 Steve Hearn - Dharma IT Ltd.
// http://www.dharma-it.com
//
// Licenced under LGPL
// GNU Library General Public License (LGPL) v2.1 Feb 1999
//
//
// More information on the SimpleAPI can be found here:
// http://www.vimeo.com/api/simple-api
//
// The library is provided on a "AS IS" basis. The beard of Steve Hearn is not repsonsible in any way 
// for whatever you use it for.

using System;
using System.IO;
using System.Net;
using System.Xml;
using System.Web;
using System.Collections.Generic;
using System.Text;

namespace Dharma.Vimeo
{
	public class SimpleAPI	{

		private string _username;
		//private string _password;
		
		/// <summary>
		/// 
		/// </summary>
		/// <param name="username">Your Vimeo Username</param>
		/// <param name="password">Your Vimeo Password</param>
		public SimpleAPI(string username)
		{
			if (username == string.Empty)
			{
				throw new ArgumentException("Username cannot be blank","username");			
			}else{
				this._username = username;
			}				
		}
		
		/// <summary>
		/// The output formats supported by the simple API.
		/// For more information about the output formats and the supported functions,
		/// check the Vimeo documentation at: http://www.vimeo.com/api/simple-api
		/// </summary>
		public enum OutputFormatType
		{
			JSON,
			XML
			//PHP  //not supported - im not that into serialized PHP arrays. ;-)
		}

		/// <summary>
		/// The various object types supported at Vimeo.
		/// </summary>
		public enum ObjectType
		{
			clips,						//Videos created by user
			subscriptions,		//Videos the user is subscribed to
			contacts_like,		//Videos that the user's contacts like
			contacts_clips,		//Videos that the user's contacts created
			appears_in,				//Videos that the user appears in
			all_user_clips		//Videos that the user appears in AND created
		}

		/// <summary>
		/// The Simple API URL format
		/// </summary>						
		protected const string VimeoBaseUrlFormat = "http://vimeo.com/api/{0}/{1}/{2}";

			
		protected string GetObjectTypeString(ObjectType objectType)
		{
			return objectType.ToString().ToLower();
		}

		protected string GetFormatTypeString(OutputFormatType format)
		{
			return format.ToString().ToLower();
		}

#region "GET Command"

		/// <summary>
		/// Executes an HTTP GET command and retrives the information.		
		/// </summary>
		/// <param name="url">The URL to perform the GET operation</param>
		/// <param name="userName">The username to use with the request</param>
		/// <param name="password">The password to use with the request</param>
		/// <returns>The response of the request, or null if we got 404 or nothing.</returns>
		protected string ExecuteGetCommand(string url, string userName, string password)
		{
			using (WebClient wc = new WebClient())
			{
				if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
				{
					wc.Credentials = new NetworkCredential(userName, password);
				}

				try
				{
					using (Stream stream = wc.OpenRead(url))
					{
						using (StreamReader reader = new StreamReader(stream))
						{
							return reader.ReadToEnd();
						}
					}
				}
				catch (WebException ex)
				{
					//
					// Handle HTTP 404 errors gracefully and return a null string to indicate there is no content.
					//
					if (ex.Response is HttpWebResponse)
					{
						if ((ex.Response as HttpWebResponse).StatusCode == HttpStatusCode.NotFound)
						{
							return null;
						}
					}

					throw ex;
				}
			}
			return null;
		}

		#endregion

		
		//Action types

#region Clips

		public string GetClips(OutputFormatType format)
		{
			string url = string.Format(VimeoBaseUrlFormat, _username,
			GetObjectTypeString(ObjectType.clips), GetFormatTypeString(format));
			return ExecuteGetCommand(url, null, null);
		}

		public string GetClipsAsJSON()
		{
			return GetClips(OutputFormatType.JSON);
		}

		public XmlDocument GetClipsAsXML(OutputFormatType format)
		{
			if (format != OutputFormatType.JSON && format != OutputFormatType.XML)
			{
				throw new ArgumentException("GetClips supports only XML and JSON", "format");
			}

			string output = GetClips(format);
			if (!string.IsNullOrEmpty(output))
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.LoadXml(output);

				return xmlDocument;
			}

			return null;
		}

		public XmlDocument GetClipsAsXML()
		{
			return GetClipsAsXML(OutputFormatType.XML);
		}
		

#endregion

#region Subscriptions

		public string GetSubscriptions(OutputFormatType format)
		{
			string url = string.Format(VimeoBaseUrlFormat, _username,
			GetObjectTypeString(ObjectType.clips), GetFormatTypeString(format));
			return ExecuteGetCommand(url, null, null);
		}

		public string GetSubscriptionsAsJSON()
		{
			return GetClips(OutputFormatType.JSON);
		}

		public XmlDocument GetSubscriptionsAsXML(OutputFormatType format)
		{
			if (format != OutputFormatType.JSON && format != OutputFormatType.XML)
			{
				throw new ArgumentException("GetSubscriptions supports only XML and JSON", "format");
			}

			string output = GetSubscriptions(format);
			if (!string.IsNullOrEmpty(output))
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.LoadXml(output);

				return xmlDocument;
			}

			return null;
		}

		public XmlDocument GetSubscriptionsAsXML()
		{
			return GetSubscriptionsAsXML(OutputFormatType.XML);
		}



		#endregion

#region Contacts Like

		public string GetContactsLike(OutputFormatType format)
		{
			string url = string.Format(VimeoBaseUrlFormat, _username,
			GetObjectTypeString(ObjectType.clips), GetFormatTypeString(format));
			return ExecuteGetCommand(url, null, null);
		}

		public string GetContactsLikeAsJSON()
		{
			return GetContactsLike(OutputFormatType.JSON);
		}

		public XmlDocument GetContactsLikeAsXML(OutputFormatType format)
		{
			if (format != OutputFormatType.JSON && format != OutputFormatType.XML)
			{
				throw new ArgumentException("GetContactsLike supports only XML and JSON", "format");
			}

			string output = GetContactsLike(format);
			if (!string.IsNullOrEmpty(output))
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.LoadXml(output);

				return xmlDocument;
			}

			return null;
		}

		public XmlDocument GetContactsLikeAsXML()
		{
			return GetContactsLikeAsXML(OutputFormatType.XML);
		}

		#endregion

#region AllUserClips

		public string GetAllUserClips(OutputFormatType format)
		{
			string url = string.Format(VimeoBaseUrlFormat, _username,
			GetObjectTypeString(ObjectType.clips), GetFormatTypeString(format));
			return ExecuteGetCommand(url, null, null);
		}

		public string GetAllUserClipsAsJSON()
		{
			return GetAllUserClips(OutputFormatType.JSON);
		}

		public XmlDocument GetAllUserClipsAsXML(OutputFormatType format)
		{
			if (format != OutputFormatType.JSON && format != OutputFormatType.XML)
			{
				throw new ArgumentException("GetAllUserClips supports only XML and JSON", "format");
			}

			string output = GetAllUserClips(format);
			if (!string.IsNullOrEmpty(output))
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.LoadXml(output);

				return xmlDocument;
			}

			return null;
		}

		public XmlDocument GetAllUserClipsAsXML()
		{
			return GetAllUserClipsAsXML(OutputFormatType.XML);
		}

#endregion
		
#region AppearsIn

		public string GetAppearsIn(OutputFormatType format)
		{
			string url = string.Format(VimeoBaseUrlFormat, _username,
			GetObjectTypeString(ObjectType.clips), GetFormatTypeString(format));
			return ExecuteGetCommand(url, null, null);
		}

		public string GetAppearsInAsJSON()
		{
			return GetAppearsIn(OutputFormatType.JSON);
		}

		public XmlDocument GetAppearsInAsXML(OutputFormatType format)
		{
			if (format != OutputFormatType.JSON && format != OutputFormatType.XML)
			{
				throw new ArgumentException("GetAppearsIn supports only XML and JSON", "format");
			}

			string output = GetAppearsIn(format);
			if (!string.IsNullOrEmpty(output))
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.LoadXml(output);

				return xmlDocument;
			}

			return null;
		}


		public XmlDocument GetAppearsInAsXML()
		{
			return GetAppearsInAsXML(OutputFormatType.XML);
		}

#endregion

#region ContactsClips

		public string GetContactsClips(OutputFormatType format)
		{
			string url = string.Format(VimeoBaseUrlFormat, _username,
			GetObjectTypeString(ObjectType.clips), GetFormatTypeString(format));
			return ExecuteGetCommand(url, null, null);
		}

		public string GetContactsClipsAsJSON()
		{
			return GetContactsClips(OutputFormatType.JSON);
		}

		public XmlDocument GetContactsClipsAsXML(OutputFormatType format)
		{
			if (format != OutputFormatType.JSON && format != OutputFormatType.XML)
			{
				throw new ArgumentException("GetContactsClips supports only XML and JSON", "format");
			}

			string output = GetContactsClips(format);
			if (!string.IsNullOrEmpty(output))
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.LoadXml(output);

				return xmlDocument;
			}

			return null;
		}


		public XmlDocument GetContactsClipsAsXML()
		{
			return GetContactsClipsAsXML(OutputFormatType.XML);
		}

#endregion
			

		
	}
}
